package com.yunyouyi.cpicture.application.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunyouyi.cpicture.domain.user.service.UserDomainService;
import com.yunyouyi.cpicture.infrastructure.common.DeleteRequest;
import com.yunyouyi.cpicture.infrastructure.exception.BusinessException;
import com.yunyouyi.cpicture.infrastructure.exception.ErrorCode;
import com.yunyouyi.cpicture.infrastructure.exception.ThrowUtils;
import com.yunyouyi.cpicture.interfaces.dto.user.UserLoginDTO;
import com.yunyouyi.cpicture.interfaces.dto.user.UserQueryDTO;
import com.yunyouyi.cpicture.interfaces.dto.user.UserRegisterDTO;
import com.yunyouyi.cpicture.domain.user.entity.User;
import com.yunyouyi.cpicture.interfaces.vo.user.UserLoginVO;
import com.yunyouyi.cpicture.interfaces.vo.user.UserVO;
import com.yunyouyi.cpicture.application.service.UserApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * @author yunyouyi
 * @description 针对表【user(用户)】的数据库操作Service实现
 * @createDate 2025-03-31 15:14:30
 */
@Service
@Slf4j
public class UserApplicationServiceImpl implements UserApplicationService {

    @Resource
    private UserDomainService userDomainService;

    /**
     * 用户注册
     * @param userRegisterDTO
     * @return
     */
    public long userRegister(UserRegisterDTO userRegisterDTO) {
        // 校验
        User.validUserRegister(userRegisterDTO);
        // 注册
        return userDomainService.userRegister(userRegisterDTO);
    }

    /**
     * 用户登录
     * @param userLoginDTO
     * @return 脱敏后的用户信息
     */
    public UserLoginVO userLogin(UserLoginDTO userLoginDTO, HttpServletRequest request) {        //2.查询用户是否存在
        // 校验登录信息
        User.validUserLogin(userLoginDTO);
        // 登录
        return userDomainService.userLogin(userLoginDTO,request);
    }

    /**
     * 获取当前登录用户
     * @param request
     * @return 当前登录用户
     */
    public User getLoginUser(HttpServletRequest request) {
        return userDomainService.getLoginUser(request);
    }

    /**
     * 获取当前登录用户（脱敏）
     * @param user
     * @return 当前登录用户（脱敏）
     */
    public UserLoginVO getLoginUserVO(User user) {
        return userDomainService.getLoginUserVO(user);
    }


    /**
     * 获取脱敏用户
     * @param user
     * @return
     */
    public UserVO getUserVO(User user) {
        return userDomainService.getUserVO(user);
    }

    /**
     * 获取脱敏用户列表
     * @param userList
     * @return
     */
    public List<UserVO> getUserVOList(List<User> userList) {
        return userDomainService.getUserVOList(userList);
    }

    /**
     * 获取加密密码
     * @param userPassword
     * @return 加密后的密码
     */
    public String getEncryptPassword(String userPassword) {
        return userDomainService.getEncryptPassword(userPassword);
    }

    /**
     * 用户退出
     * @param request
     */
    public Boolean userLogout(HttpServletRequest request) {
        return userDomainService.userLogout(request);
    }


    /**
     * 处理用户信息
     * @param user
     * @return 脱敏后的用户信息
     */
    public UserLoginVO handleUser(User user){
        UserLoginVO userLoginVO = new UserLoginVO();
        BeanUtils.copyProperties(user,userLoginVO);
        return userLoginVO;
    }

    /**
     * 获取查询条件
     * @param userQueryDTO
     * @return
     */
    public QueryWrapper<User> getQueryWrapper(UserQueryDTO userQueryDTO) {
        return userDomainService.getQueryWrapper(userQueryDTO);
    }

    @Override
    public User getUserById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        User user = userDomainService.getById(id);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
        return user;
    }

    @Override
    public UserVO getUserVOById(long id) {
        return userDomainService.getUserVO(getUserById(id));
    }

    @Override
    public boolean deleteUser(DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return userDomainService.removeById(deleteRequest.getId());
    }

    @Override
    public void updateUser(User user) {
        boolean result = userDomainService.updateById(user);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
    }

    @Override
    public Page<UserVO> listUserVOByPage(UserQueryDTO userQueryDTO) {
        ThrowUtils.throwIf(userQueryDTO == null, ErrorCode.PARAMS_ERROR);
        long current = userQueryDTO.getCurrent();
        long size = userQueryDTO.getPageSize();
        Page<User> userPage = userDomainService.page(new Page<>(current, size),
                userDomainService.getQueryWrapper(userQueryDTO));
        Page<UserVO> userVOPage = new Page<>(current, size, userPage.getTotal());
        List<UserVO> userVO = userDomainService.getUserVOList(userPage.getRecords());
        userVOPage.setRecords(userVO);
        return userVOPage;
    }

    @Override
    public List<User> listByIds(Set<Long> userIdSet) {
        return userDomainService.listByIds(userIdSet);
    }

    @Override
    public long saveUser(User userEntity) {
        final String DEFAULT_PASSWORD = "12361224";
        String encryptPassword = userDomainService.getEncryptPassword(DEFAULT_PASSWORD);
        userEntity.setUserPassword(encryptPassword);
        boolean result = userDomainService.saveUser(userEntity);
        ThrowUtils.throwIf(!result, ErrorCode.SYSTEM_ERROR,"注册失败");
        return userEntity.getId();
    }
}




